using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(Waypoint))]
public class WaypointEditor : Editor
{
    Waypoint Waypoint => target as Waypoint; // 获取目标 Waypoint 组件

    // 在Scene视图中绘制编辑器
    private void OnSceneGUI()
    {
        Handles.color = Color.red;

        // 遍历路径点
        for (int i = 0; i < Waypoint.Points.Length; i++)
        {
            EditorGUI.BeginChangeCheck();
            
            // 创建可移动的 Handles
            Vector3 currentWaypointPoint = Waypoint.CurrentPosition + Waypoint.Points[i];
            var fmh_20_17_638355057726425685 = Quaternion.identity; Vector3 newWaypointPoint = Handles.FreeMoveHandle(currentWaypointPoint,
                0.7f,
                new Vector3(0.3f, 0.3f, 0.3f),
                Handles.SphereHandleCap
            );

            // 创建文本样式
            GUIStyle textStyle = new GUIStyle();
            textStyle.fontStyle = FontStyle.Bold;
            textStyle.fontSize = 16;
            textStyle.normal.textColor = Color.yellow;

            // 设置文本位置
            Vector3 textAlignment = new Vector3(0, -0.35f, 0.35f);

            // 在Scene视图中创建标签文本
            Handles.Label(
                position: Waypoint.CurrentPosition + Waypoint.Points[i] + textAlignment,
                text: $"{i + 1}", // 显示路径点的索引（从1开始）
                style: textStyle
            );

            EditorGUI.EndChangeCheck();

            // 检查编辑是否结束并且有变化
            if (EditorGUI.EndChangeCheck())
            {
                // 在Undo系统中记录对象状态，以便进行撤销操作
                Undo.RecordObject(target, "Free Move Handle");

                // 更新路径点的位置为新的位置
                Waypoint.Points[i] = newWaypointPoint - Waypoint.CurrentPosition;
            }
        }
    }
}